package com.dh.example.netty.util;


import com.dh.example.netty.enums.WsRespTypeEnum;
import com.dh.example.netty.entity.User;
import com.dh.example.netty.pojo.response.WsBaseResp;
import com.dh.example.netty.pojo.response.support.WsMsgLoginSuccess;
import com.dh.example.netty.pojo.response.support.WsMsgLoginUrl;

/**
 * ws msg builder适配器
 *
 * @author yolo
 * @date 2024/03/14 15:03:48
 */
public class WsMsgBuilderAdapter {


    public static WsBaseResp<WsMsgLoginUrl> buildLoginResp(String url) {
        WsBaseResp<WsMsgLoginUrl> wsBaseResp = new WsBaseResp<>();
        wsBaseResp.setEventType(WsRespTypeEnum.LOGIN_URL_RESP.getType());
        wsBaseResp.setData(WsMsgLoginUrl.builder().loginUrl(url).build());
        return wsBaseResp;
    }

    public static WsBaseResp buildHeartBeatResp() {
        WsBaseResp wsBaseResp = new WsBaseResp();
        wsBaseResp.setEventType(WsRespTypeEnum.PING.getType());
        return wsBaseResp;
    }

    public static WsBaseResp<String> buildInvalidateTokenResp() {
        WsBaseResp<String> wsBaseResp = new WsBaseResp<>();
        wsBaseResp.setEventType(WsRespTypeEnum.INVALIDATE_TOKEN.getType());
        wsBaseResp.setData("token认证失败，请重新登录");
        return wsBaseResp;
    }

    public static WsBaseResp<WsMsgLoginSuccess> buildLoginSuccessResp(User user, String token, boolean hasPower) {
        WsBaseResp<WsMsgLoginSuccess> wsBaseResp = new WsBaseResp<>();
        wsBaseResp.setEventType(WsRespTypeEnum.LOGIN_SUCCESS.getType());
        WsMsgLoginSuccess wsLoginSuccess = WsMsgLoginSuccess.builder()
                .avatar(user.getAvatar())
                .name(user.getName())
                .token(token)
                .userId(user.getId())
                .power(hasPower ? 1 : 0)
                .build();
        wsBaseResp.setData(wsLoginSuccess);
        return wsBaseResp;
    }

    public static WsBaseResp<String> buildOfflineNotifyResp(User user) {
        WsBaseResp<String> wsBaseResp = new WsBaseResp<>();
        wsBaseResp.setEventType(WsRespTypeEnum.USER_ACCOUNT_CHANGE.getType());
        wsBaseResp.setData(String.format("用户下线[%s]", user.getName()));
        return wsBaseResp;
    }


    /**
     * 构建系统msg resp
     *
     * @param data 数据
     * @return {@code WsBaseResp<String>}
     */
    public static WsBaseResp<String> buildSystemMsgResp(String data) {
        WsBaseResp<String> wsBaseResp = new WsBaseResp<>();
        wsBaseResp.setEventType(WsRespTypeEnum.SYSTEM_MESSAGE_NOTIFICATION.getType());
        wsBaseResp.setData(data);
        return wsBaseResp;
    }
}
